package cn.kgc.controller.user;

import cn.kgc.pojo.Quanxianinfo;
import cn.kgc.pojo.User;
import cn.kgc.pojo.Zhiweiquanxian;
import cn.kgc.service.system.IuserService;
import cn.kgc.service.system.smj_logService;
import cn.kgc.service.system.smj_zwqxService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Controller
@RequestMapping("/userLogin")
public class userController {

    @Resource
    private smj_logService logService;

    //@Autowired注解和@Resource的作用相同
    @Autowired
    private IuserService userService;
    @Resource
    public smj_zwqxService zwqx;

    @Resource
    private IuserService iuserService;





    @RequestMapping(value = "/dengLu", method = RequestMethod.GET)
    public String toLogin() {
        return "/login";
    }

    @RequestMapping("/toLogin")
    @ResponseBody
    public Map login(@RequestParam String username, @RequestParam String password, HttpSession session) {
        System.out.println("username = " + username);
        Map map = new HashMap();
        User login = userService.login(username, password);
        if (login != null) {
            if (login.getState() != 0) {
                Zhiweiquanxian zhiweiquanxian = zwqx.selJobQXByJbId(login.getJt_id());
                List<Integer> lists = splitStr(zhiweiquanxian.getAuths());
                for (int i = 0; i < lists.size(); i++) {
                    System.out.println("list.get(i)====================================  = " + lists.get(i).equals(17));
                    if (lists.get(i) == 17 || lists.get(i) == 18 || lists.get(i) == 19 || lists.get(i) == 20 || lists.get(i) == 21) {

                        try {
                            logService.insertLog(login.getId(), "账户登录--系统设置端");//记录事件
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        System.out.println("i!!!!!!!! = " + i);
                        //取到登录的jt_id返回职位下的1-2-3-4-5
                        Zhiweiquanxian zwqx1 = zwqx.selJobQXByJbId(login.getJt_id());
                        //根据1-2-3取具体权限信息
                        List<Quanxianinfo> quanxianinfos = zwqx.selQXPathByAuths(splitStr(zwqx1.getAuths()));
                        //放入session中权限信息
                        session.setAttribute("quanxianinfos2", JSON.toJSONString(quanxianinfos));
                        //登录的信息放入session
                        session.setAttribute("success", login);
                        map.put("error", "success");
                        break;
                    } else {
                        map.put("error", "您好，您的权限不足，请联系管理员！");
                    }
                }
            } else {
                map.put("error", "您的账号已被冻结，请联系管理员！");
            }
        } else {
            map.put("error", "您的密码或者账号错误，请重新输入！");
        }
        return map;
    }


    @RequestMapping(value = "/index")
    public String index() {
        return "/index";
    }


    /**
     * 分割字符串方法
     */
    public List splitStr(String str) {
        List list = new ArrayList();
        String[] split = str.split("-");

        for (int i = 0; i < split.length; i++) {
            if (split[i] != "0") {
                System.out.println("split[i] = " + split[i]);
                list.add(Integer.valueOf(split[i]));
            }
        }
        if (list.size() == 0) {
            list.add(-1);
        }
        return list;
    }

    /**
     * 修改密码
     */
    @RequestMapping("/updatePwd")
    @ResponseBody
    public Boolean updatePwd(Integer id, String pwd) {

        try {
            logService.insertLog(id, "修改了密码");
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        Integer integer = iuserService.updatePwd(id, pwd);
        if (integer > 0) {
            return true;
        }
        return false;
    }

    /**
     *个人信息
     */
    @RequestMapping("/selUserInfo")
    @ResponseBody
    public User selUserInfo(Integer id ){
        User users = iuserService.selUserInfo(id);
        return users;
    }


}
